//
//  MapViewViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/17.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "MapViewViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "ACMacros.h"
#import "Constants.h"

@interface MapViewViewController () <MAMapViewDelegate, AMapSearchDelegate>

@end

@implementation MapViewViewController {
    MAMapView *mapView;
    AMapSearchAPI *mapSearch;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNavigationBar];
    if (self.Latitude && self.Longitude) {
        [self createMapView];
    } else {
        if (self.communityName || ![self.communityName isEqual:@""]) {
            [self createMapView];
            [self createSearch];
        }
    }
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)configureNavigationBar {
    self.title = @"小区地图";
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToPopBack)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)createMapView {
    [MAMapServices sharedServices].apiKey = kAmapKey;
    mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [self.view addSubview:mapView];
    mapView.delegate = self;
    mapView.showsUserLocation = true;
    [mapView setCenterCoordinate:CLLocationCoordinate2DMake(self.Latitude, self.Longitude) animated:true];
    [mapView setZoomLevel:15.0 animated:true];
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(self.Latitude, self.Longitude);
    pointAnnotation.title = @"小区位置";
    
    [mapView addAnnotation:pointAnnotation];
}

- (void)createSearch {
    mapSearch = [[AMapSearchAPI alloc]init];
    [mapSearch setDelegate:self];
    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
    geo.address = [NSString stringWithFormat:@"厦门%@",self.communityName];
    [mapSearch AMapGeocodeSearch:geo];

}

#pragma mark - Triggering Methods
- (void)clickToPopBack {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)searchWithName:(NSString *)name {
    
}

- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    if(response.geocodes.count == 0)
    {
        return;
    }
    if (response.geocodes.count == 0) {
        
    } else {
        AMapTip *p = response.geocodes.firstObject;
        self.Latitude = p.location.latitude;
        self.Longitude = p.location.longitude;
        [self createMapView];
    }
    
    //通过AMapGeocodeSearchResponse对象处理搜索结果
}

@end
